//获取应用实例
var app = getApp();
Page({
    data: {
        platform:"iPhone",
        isYueS:false
    },
    onLoad: function () {
        var data = app.getStorageSync(app.globalData.cacheOrderResKey);
        this.setData({
            "isYueS":app.isYueS(data.carNumPrefix),
            "serviceFeeDesc":data.serviceFeeDesc,
            "serviceFee":data.serviceFee,
            "carNumber":data.carNumber,
            "carCode":data.carCode,
            "cardName":data.cardName,
            "cardNum":data.cardNum,
            "telephone":data.telephone,
            "cardPicture":data.cardPicture,
            "policyPicture":data.policyPicture,


            "address.street":data['address.street'],
            "address.provinceId":data['address.provinceId'],
            "address.cityId":data['address.cityId'],
            "address.countyId":data['address.countyId'],
            "address.provinceName":data['address.provinceName'],
            "address.cityName":data['address.cityName'],
            "address.countyName":data['address.countyName'],
            "address.cellNum":data['address.cellNum'],
            "address.contactName":data['address.contactName'],
            "address.fullAddr":data['address.fullAddr'],
            "needInvoice":data.needInvoice
        });
        if(app.isAndroid()){
            this.setData({platform:"android"});
        }
    },
    //预览上传图片
    viewUploadImgTap:function (e) {
        var uploadImgSrc = e.currentTarget.dataset.src;
        var opt = {
            urls:[uploadImgSrc],
            success:function (res) {
                console.log(res);
            }
        };
        app.previewImage(opt)
    },
    formSubmit:function (e) {
        var pageObj = this;
        var param = app.getStorageSync(app.globalData.cacheOrderResKey);
        var platform = this.data.platform;
        param['platform'] = this.data.platform;
        param['formId'] = e.detail.formId;
        param['payType'] = 3;
        if(app.isYueS(param.carNumPrefix)){
            param['payType'] = 0;
        }
        param['accountId'] = app.globalData.openId;
        var opt = {
            url: app.globalData.host+"/wx/an/pay.ajax",
            data: param,
            sucFn:function (result) {
                if(param['payType'] == 0){//到付
                    var orderParam = app.getStorageSync(app.globalData.cacheOrderResKey);
                    orderParam['orderId'] = result.body.orderIds[0].id;
                    orderParam['chargeOrderId'] = result.body.orderIds[0].chargeOrderId;
                    app.setStorageSync(app.globalData.cacheOrderResKey, orderParam);
                    pageObj.handlePageCache();
                    app.redirectTo({ url: '../paysuccess/paysuccess' });
                    return false;
                }
                //微信支付
                var payParam = {
                    'timeStamp': result.body.wxPayInfo.timeStamp,
                    'nonceStr': result.body.wxPayInfo.nonceStr,
                    'package': result.body.wxPayInfo.packageName,
                    'signType':result.body.wxPayInfo.signType,
                    'paySign': result.body.wxPayInfo.paySign,
                    sucFn: function (successResult) {
                        var orderParam = app.getStorageSync(app.globalData.cacheOrderResKey);
                        orderParam["orderId"] = result.body.orderIds[0].id;
                        orderParam["chargeOrderId"] = result.body.orderIds[0].chargeOrderId;
                        orderParam['prepayId'] = result.body.wxPayInfo.prepayId;
                        orderParam['paySign'] = result.body.wxPayInfo.paySign;
                        orderParam['timeStamp'] = result.body.wxPayInfo.timeStamp;
                        app.setStorageSync(app.globalData.cacheOrderResKey, orderParam);

                        var updateOpt = {
                            url: app.globalData.host + "/wx/an/payCallback.ajax",
                            data: {
                                'orderId': orderParam["chargeOrderId"],
                                'prepayId': orderParam['prepayId'],
                                'moduleOrderId': orderParam['orderId'],
                                'platform':platform
                            },
                            sucFn: function (result) {
                                pageObj.handlePageCache();
                                app.redirectTo({ url: '../paysuccess/paysuccess' });
                            }
                        };
                        app.ajax(updateOpt);
                    },
                    failFn: function (failResult) {
                        console.log(failResult);
                    },
                };
                app.requestPayment(payParam);
            }
        };
        app.ajax(opt);
    },
    //下单成功清除其他信息
    handlePageCache:function(){
        var obj = app.getStorageSync(app.globalData.cachePageKey);
        obj.num = obj.num + 1;
        obj.carNumPrefix = null;
        obj.carNumberSuffix = null;
        obj.carCode = null;
        obj.cardName = null;
        obj.cardNum = null;
        obj.telephone = null;
        obj['verifyCodeClear'] = '1';
        obj.cardPicture = "";
        obj.policyPicture = "";
        app.setStorageSync(app.globalData.cardPictureKey,"");
        app.setStorageSync(app.globalData.policyPictureKey,"")
        app.setStorageSync(app.globalData.cachePageKey, obj);
    }
});
